/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.uhk.ppro.response.web;

import cz.uhk.ppro.response.model.Answer;
import cz.uhk.ppro.response.model.Question;
import cz.uhk.ppro.response.model.Test;
import cz.uhk.ppro.response.service.DatabaseService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Martin Zmítko
 */
@Controller
@SessionAttributes({"testData"})
public class CommonController {

    @Autowired
    private DatabaseService databaseSession;

    @RequestMapping("/intro.do")
    public ModelAndView zobrazit()
    {
        ModelAndView model = new ModelAndView("intro");

        return model;
    }


    @RequestMapping("/resources.do")
    public ModelAndView showMaterials()
    {
        ModelAndView model = new ModelAndView("resources");

        return model;
    }

    @RequestMapping("/graph.do")
    public ModelAndView showGraphs()
    {
        ModelAndView model = new ModelAndView("graph");

        return model;
    }

    @RequestMapping(value="tests_questions_edit.do", method = RequestMethod.GET)
    protected String setupDruhyForm(@RequestParam(value="id",required=false) Integer id, Model m)
    {
        Test test = databaseSession.getTestById(id);
        List<Question> questions = databaseSession.getQuestions(test);
        for(Question q : questions) {
            List<Answer> answers = databaseSession.getAnswers(q);
            q.setAnswers(answers);
        }
        test.setQuestions(questions);

        m.addAttribute("testData", test);
        m.addAttribute("test_edit", true);

        return "formTestPartTwo";
    }

    @RequestMapping(value = "tests_questions_edit.do", method = RequestMethod.POST)
    protected String submit(@ModelAttribute("testData") Test test, BindingResult result)
    {
        databaseSession.formEditOtazkyOpdovedi(test);
        return "redirect:/tests.do";
    }





}
